<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;

/**
 * Class BuySupplierCount
 * @package app\home\controller
 * 采购供应商汇总
 */
class BuySupplierCount extends BaseController
{
    public function index()
    {
        if (request()->isAjax()) {
            $param=$this->param;
            $where[]=['ird.status','=',2];
            $where[]=['ir.type','=','采购收货'];
            $where[]=['ir.punter_id','>',0];
            if (isset($param['bar_code'])&&$param['bar_code']){
                $where[]=['md.bar_code','like','%'.$param['bar_code']];
            }
            if (isset($param['mdName'])&&$param['mdName']){
                $where[]=['md.name','like','%'.$param['mdName'].'%'];
            }
            if (isset($param['pName'])&&$param['pName']){
                $where[]=['p.name','like','%'.$param['pName'].'%'];
            }
            if (isset($param['orderDate'])&&$param['orderDate']){
                $dates=explode('~',$param['orderDate']);
                $where[]=['ir.incomTime','between',$dates];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list= Db::name('in_receipt_detail')->alias('ird')
                ->leftJoin('in_receipt ir','ir.id=ird.in_receipt_id')
                ->leftJoin('purveyor p ','p.id=ir.punter_id')
                ->leftJoin('material_dossier md','md.id=ird.material_dossier_id')
                ->where($where)
                ->field('ird.material_dossier_id,ird.in_receipt_id,ir.punter_id,p.name as pName,md.name as mdName,p.code as pCode,md.bar_code,md.specs,md.unit,sum(ird.num) as saleNum')
                ->group('p.id,ird.material_dossier_id')
                ->paginate($rows);
//            var_dump(Db::name('in_receipt_detail')->getLastSql());exit();
            return table_assign(0, '', $list);
        }
        return view();
    }
}